package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem;

import android.content.Context;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.dal.UserInfoDAL;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.database.DBHelper;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.User;

@RunWith(AndroidJUnit4.class)
public class UserInfoDALTest {
    private Context context;
    private UserInfoDAL userInfoDAL;

    @Before
    public void setUp() {
        context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        DBHelper dbHelper = new DBHelper(context, true);
        userInfoDAL = new UserInfoDAL(context);
    }

    @Test
    public void testAddUser() {
        User user = new User("testUser", "testPass", "testEmail@example.com", 1);
        long userId = userInfoDAL.addUser(user);
        Assert.assertTrue(userId > 0);
    }

//    @Test
//    public void testLogin() {
//        User user = new User("testUser", "testPass", "testEmail@example.com", 1);
//        userInfoDAL.addUser(user);
//        Integer[] loginResult = userInfoDAL.login("testUser", "testPass");
//        Assert.assertNotNull(loginResult);
//        Assert.assertEquals(1, loginResult[1].intValue()); // role
//    }
//
//    @Test
//    public void testGetUserInfo() {
//        User user = new User("testUser", "testPass", "testEmail@example.com", 1);
//        userInfoDAL.addUser(user);
//        Integer[] loginResult = userInfoDAL.login("testUser", "testPass");
//        Integer userId = loginResult[0];
//        User result = userInfoDAL.getUserInfoById(userId);
//        Assert.assertNotNull(result);
//        Assert.assertEquals("testUser", result.getUsername());
//        Assert.assertEquals("testPass", result.getPassword());
//        Assert.assertEquals("testEmail@example.com", result.getEmail());
//        Assert.assertEquals(1, result.getRole().intValue());
//    }
//
//    @Test
//    public void testUpdateUserInfo() {
//        User user = new User("testUser", "testPass", "testEmail@example.com", 1);
//        userInfoDAL.addUser(user);
//        Integer[] loginResult = userInfoDAL.login("testUser", "testPass");
//        Integer userId = loginResult[0];
//        User newUser = new User(userId, "testUserNew", "testPassNew", "testEmail@example.com", 1);
//        userInfoDAL.updateUserInfo(newUser);
//        User result = userInfoDAL.getUserInfoById(userId);
//        Assert.assertNotNull(result);
//        Assert.assertEquals("testUserNew", result.getUsername());
//        Assert.assertEquals("testPassNew", result.getPassword());
//        Assert.assertEquals("testEmail@example.com", result.getEmail());
//        Assert.assertEquals(1, result.getRole().intValue());
//    }
}
